import pymysql
from ddt import ddt, data, unpack
from selenium import webdriver
from selenium.webdriver.common.by import By
import Data
import Operation
import time
from unittest import TestCase

@ddt
class TestBuyQiXian(TestCase):
    @data(*Data.Buy_LingQian.right)
    @unpack
    def test_buy_jijin(self, name, pwd, paywd, expect):
        print(name,pwd,paywd,expect)
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=10000 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()

        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        driver.maximize_window()
        buy = Operation.BuyJiJin()
        buy.buy_jijin(name,pwd,paywd,driver)
        result = buy.buy_jijin_result(driver)
        if result[0] != expect or result[1] != '账户余额：7000.00元':
            driver.save_screenshot(f'{name},{pwd},{paywd}.jpg')
        self.assertEqual(expect, result[0])
        self.assertEqual('账户余额：7000.00元', result[1])

    @data(*Data.Buy_LingQian.wrong_paywd)
    @unpack
    def test_wrongpaywd(self, name, pwd, paywd, expect):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        buy = Operation.BuyJiJin()
        buy.buy_wrongpaywd(name, pwd, paywd, driver)
        time.sleep(0.5)

        result = buy.buy_wrongpaywd_result(driver)
        if result != expect:
            driver.save_screenshot(f'{name},{pwd},{paywd}.jpg')
        self.assertEqual(expect, result)

    @data(*Data.Buy_LingQian.no_money)
    @unpack
    def test_nomoney(self, name, pwd, paywd, expect):
        connect = pymysql.connect(host='localhost', user='root', password='root', database='finance')
        cursor = connect.cursor()
        sql = "select username,id from user"
        cursor.execute(sql)
        x = cursor.fetchall()
        for i in range(len(x)):
            if 'lisi4' in x[i - 1]:
                userid = x[i - 1][1]
        sql = f"update bankcard set balance=99 where userId={userid} and defaultl=1"
        cursor.execute(sql)
        connect.commit()

        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        buy = Operation.BuyJiJin()
        buy.buy_nomoney(name, pwd, paywd, driver)
        result = buy.buy_nomoney_result(driver)
        if result != expect:
            driver.save_screenshot(f'{name},{pwd},{paywd}.jpg')
        self.assertEqual(expect, result)

    @data(*Data.Buy_LingQian.no_card)
    @unpack
    def test_nocard(self, name, pwd, paywd, expect):
        driver = webdriver.Chrome()
        driver.get("http://localhost:90")
        buy = Operation.BuyJiJin()
        buy.buy_nocard(name, pwd, paywd, driver)
        result = buy.buy_nocard_result(driver)
        if result != expect:
            driver.save_screenshot(f'{name},{pwd},{paywd}.jpg')
        self.assertEqual(expect, result)